package xyz.scootaloo.draft.simple;

import cn.hutool.core.io.file.FileReader;

import java.io.File;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @author flutterdash@qq.com
 * @since 2021/6/30 16:45
 */
public class NameSearch {
    static class Stu {
        String name;
        boolean sex;
        public Stu(String name, boolean sex) {
            this.name = name;
            this.sex = sex;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Stu stu = (Stu) o;
            return sex == stu.sex && Objects.equals(name, stu.name);
        }

        @Override
        public int hashCode() {
            return name.hashCode();
        }
    }
    static String[] names = {
            "陈德民",
            "徐俊瑞",
            "严德强",
            "曹鹏",
            "李婷_",
            "赵建华_",
            "王瑞瑞_",
            "谢丹_",
            "程龙",
            "张海东",
            "金凡其",
            "刘小婉_",
            "梁子健",
            "李舟舟_",
            "阮微_",
            "万炎清",
            "熊清华_",
            "刘子康",
            "付瀛斌",
            "饶青钢",
            "詹豪迪",
            "张时中",
            "闵俊龙",
            "付明旺",
            "付罗稳",
            "张俊鹏",
            "信博华",
            "张小渝",
            "严震波",
            "陈志浩",
            "周子寒",
            "田信",
            "王勃文",
            "简懿枫",
            "肖宇浩",
            "王伟康",
            "安依然",
            "郑文爽_",
            "尹宏杨_",
            "黄俏俏_",
            "张美婷_",
            "陈怡樾_",
            "罗莹_",
            "万习婷_",
            "邵李静_"
    };

    public static void main(String[] args) {
        Set<Stu> students = reduce();
        statistics(students);
        search(students);
    }

    private static Set<Stu> reduce() {
        Set<Stu> students = new HashSet<>();
        for (String stu : names) {
            if (stu.endsWith("_")) {
                students.add(new Stu(stu.substring(0, stu.length() - 1), false));
            } else {
                students.add(new Stu(stu, true));
            }
        }
        return students;
    }

    private static void statistics(Set<Stu> students) {
        int m = 0;
        int f = 0;
        for (Stu stu : students) {
            if (stu.sex)
                m++;
            else
                f++;
        }
        log("人数: " + students.size());
        log("男生人数: " + m);
        log("女生人数: " + f);
    }

    private static void search(Set<Stu> students) {
        File source = new File(Objects.requireNonNull(
                NameSearch.class.getClassLoader()
                        .getResource("draft/list.txt")).getFile());
        FileReader fileReader = new FileReader(source);
        String text = fileReader.readString();
        for (Stu stu : students) {
            if (text.contains(stu.name)) {
                log(stu.name);
            }
        }
    }

    private static void log(String msg) {
        System.out.println(msg);
    }

}
